package control.snmp;

import java.io.IOException;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;


public class Teste {
	// EH ESSE O NUMERO DO GET ?	
	private static final String OID_GET = "1.3.6.1.2.1.1.1.0";
	
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		try {
			//ENDERECO			
			Address address = GenericAddress.parse("10.0.0.1");
			// PRA Q ? APARENTEMENTE EH O EQUIPAMENTO QUE SE QUER RESPOSTA
			CommunityTarget communityTarget = new CommunityTarget(address, new OctetString("public"));
			//V2 DO SNMP			
			communityTarget.setVersion(SnmpConstants.version2c);
			communityTarget.setRetries(2);
			communityTarget.setTimeout(25);
			// PRA Q ?			
			TransportMapping transport = new DefaultUdpTransportMapping();
			transport.listen();
			//IDENTIFICADOR			
			OID oid = new OID(OID_GET);
			//PDU ?			
			PDU pdu = new PDU();
			//VARIABLE BINDING ? COMO SE FOSSE O OBJETO DE COMUNICACAO			
			pdu.add(new VariableBinding(oid));
			pdu.setType(PDU.GET);
			//OBJETO SNMP			
			Snmp snmp = new Snmp(transport);
			//REQUISICAO			
			ResponseEvent response = snmp.get(pdu, communityTarget);
			if (response != null) {
				// PRA Q ? APARENTEMENTE EH A RESPOSTA DO AGENTE	
				PDU responsePDU = response.getRequest();
				
				System.out.println("***** ERROR ? *****");
				System.out.println(responsePDU.getErrorStatusText());
				System.out.println("*******************");
				System.out.println("***** VARIABLE BIDINGS *****");
				for (VariableBinding v : responsePDU.getVariableBindings()) {
					System.out.println(v.getVariable().toString());
				}
				System.out.println("****************************");
			}
			snmp.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
